package com.example.app06_component;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 简单组件Activity
 *
 * @author lijiguo
 */
public class SimpleComponentActivity extends AppCompatActivity {

    private static final String TAG = "SimpleComponentActivity";

    private CheckBox cbSimpleBasketball;
    private CheckBox cbSimpleFootball;
    private CheckBox cbSimplePingpang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_component);

        Log.i(TAG, "onCreate()");

        TextView tvSimpleMessage = findViewById(R.id.tv_simple_message);
        tvSimpleMessage.setText("简单组件TextView");

        Button btnSimpleSubmit = findViewById(R.id.btn_simple_submit);
        EditText etSimplePhone = findViewById(R.id.et_simple_phone);
        EditText etSimplePassword = findViewById(R.id.et_simple_password);
        btnSimpleSubmit.setOnClickListener(v -> {
            // 获取输入的内容
            String phoneNumber = etSimplePhone.getText().toString();
            String password = etSimplePassword.getText().toString();
            // 显示
            Toast.makeText(SimpleComponentActivity.this,
                    "用户名：" + phoneNumber + "，密码：" + password, Toast.LENGTH_LONG)
                    .show();
        });

        ImageView ivSimpleDog = findViewById(R.id.iv_simple_dog);
        ivSimpleDog.setOnClickListener(v -> {
            // 设置背景图片
            v.setBackgroundResource(android.R.drawable.alert_dark_frame);
        });

        // 初始化CheckBox
        cbSimpleBasketball = findViewById(R.id.cb_simple_basketball);
        cbSimpleFootball = findViewById(R.id.cb_simple_football);
        cbSimplePingpang = findViewById(R.id.cb_simple_pinpangball);
        // 给cbSimplePingpang设置选中状态改变监听
        cbSimplePingpang.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(SimpleComponentActivity.this, "选中乒乓球",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(SimpleComponentActivity.this, "取消选中乒乓球",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 初始化 RadioGroup RadioButton
        RadioGroup rgSimpleGender = findViewById(R.id.rg_simple_gender);
        rgSimpleGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                String gender = radioButton.getText().toString();
                Toast.makeText(SimpleComponentActivity.this, gender,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 确定按钮点击事件
     * @param view 发生事件的视图对象
     */
    public void confirm(View view) {
        List<String> hobby = new ArrayList<>(3);
        if (cbSimpleBasketball.isChecked()) {
            hobby.add(cbSimpleBasketball.getText().toString());
        }

        if (cbSimpleFootball.isChecked()) {
            hobby.add(cbSimpleFootball.getText().toString());
        }

        if (cbSimplePingpang.isChecked()) {
            hobby.add(cbSimplePingpang.getText().toString());
        }
        String hobbyStr = "您选择的爱好是：" + String.join(",", hobby);
        Toast.makeText(this, hobbyStr, Toast.LENGTH_SHORT).show();
    }
}